package wearingparts

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/wearingparts"
	wearingpartsReq "github.com/flipped-aurora/gin-vue-admin/server/model/wearingparts/request"
	"gorm.io/gorm"
)

type ErotableService struct{}

// CreateErotable 创建易损件信息记录
// Author [yourname](https://github.com/yourname)
func (ETService *ErotableService) CreateErotable(ctx context.Context, ET *wearingparts.Erotable) (err error) {
	err = global.GVA_DB.Create(ET).Error
	return err
}

// DeleteErotable 删除易损件信息记录
// Author [yourname](https://github.com/yourname)
func (ETService *ErotableService) DeleteErotable(ctx context.Context, ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&wearingparts.Erotable{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&wearingparts.Erotable{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteErotableByIds 批量删除易损件信息记录
// Author [yourname](https://github.com/yourname)
func (ETService *ErotableService) DeleteErotableByIds(ctx context.Context, IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&wearingparts.Erotable{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&wearingparts.Erotable{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateErotable 更新易损件信息记录
// Author [yourname](https://github.com/yourname)
func (ETService *ErotableService) UpdateErotable(ctx context.Context, ET wearingparts.Erotable) (err error) {
	err = global.GVA_DB.Model(&wearingparts.Erotable{}).Where("id = ?", ET.ID).Updates(&ET).Error
	return err
}

// GetErotable 根据ID获取易损件信息记录
// Author [yourname](https://github.com/yourname)
func (ETService *ErotableService) GetErotable(ctx context.Context, ID string) (ET wearingparts.Erotable, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&ET).Error
	return
}

// GetErotableInfoList 分页获取易损件信息记录
// Author [yourname](https://github.com/yourname)
func (ETService *ErotableService) GetErotableInfoList(ctx context.Context, info wearingpartsReq.ErotableSearch) (list []wearingparts.Erotable, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&wearingparts.Erotable{})
	var ETs []wearingparts.Erotable
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&ETs).Error
	return ETs, total, err
}
func (ETService *ErotableService) GetErotablePublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
